/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-07-23 10:35:32
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-21 15:12:13
 * @FilePath: /vue3-uniapp/src/utils/route.ts
 */

/**
 * 获取当前页面
 * @returns
 */
export const getCurrentPage = <
    T extends {
        options: Record<string, unknown>;
    }
>() => {
    const pages = getCurrentPages<T>();
    return pages[pages.length - 1];
};

/**
 * 获取当前页面route(格式类似这样pages/auth/LoginView/LoginView)
 * @returns
 */
export const getCurrentRoute = () => {
    const page = getCurrentPage();
    const currentRoute = `/${page.route}`;
    if (!currentRoute) {
        throw new Error(
            '[src/utils/route]getCurrentPages获取不到当前页面route'
        );
    }
    return currentRoute;
};

export const getPage = <
    T extends {
        options: Record<string, unknown>;
    }
>(
    index: number
): (Page.PageInstance<AnyObject, T> & T) | undefined => {
    const pages = getCurrentPages<T>();
    return pages[index >= 0 ? index : pages.length + index];
};
